package com.fm.Utils;


import com.fm.Service_run;

import java.io.*;
import java.util.Properties;
import java.util.Set;

public class utils {


    /**
     * 写入配置文件
     *
     * @param key
     * @param value
     * @throws IOException
     */
    public static void writePropertyData(String key, String value) throws IOException {
        String filePath = "src/main/resources/config.properties";
        Properties properties = new Properties();
        properties.load(new FileInputStream(filePath));
        properties.setProperty(key, value);
        OutputStream output = new FileOutputStream(filePath);
        properties.store(output, null);
        output.close();
    }


    public static void main(String[] args) throws IOException {
        InputStream inputStream = Service_run.class.getClassLoader().getResourceAsStream("config.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        Set<String> strings = properties.stringPropertyNames();
        System.out.println(strings);
    }



    /**
     * 获取Properties值
     *
     * @param Key
     * @return
     */
    public static String getPropertiesFileData(String Key) {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src/main/resources/config.properties"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 读取属性
        return properties.getProperty(Key);
    }


    //输入流
    public static StringBuilder printInputStreamText(InputStream inputStream) {
        byte[] bytes = new byte[1024];
        int len = 0;
        StringBuilder s = new StringBuilder();
        while (true) {
            try {
                if ((len = inputStream.read(bytes)) == -1) break;  //里面为空的时候为-1 将读取到的长度给len
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            s.append(new String(bytes, 0, len));
            System.out.print(s);
        }
        return s;
    }

    //发送 输入流文件
    public static void sendOutputStreamText(OutputStream outputStream, String text) {
        try {
            outputStream.write(text.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void sendOutputStreamFile(OutputStream outputStream, String FilePath) {
        // 读取本地文件，并通过输出流发送给服务器
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(FilePath);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        byte[] buffer = new byte[1024];
        int bytesRead;

        while (true) {
            try {
                if (!((bytesRead = fileInputStream.read(buffer)) != -1)) break;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                outputStream.write(buffer, 0, bytesRead);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }


}
